package com.dragonsky.shop.common.exception;

import com.dragonsky.shop.common.utils.AjaxResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public AjaxResult<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        e.printStackTrace();
        return AjaxResult.failed(e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(value = Exception.class)
    public AjaxResult<Void> handleException(Exception e) {
        e.printStackTrace();
        return AjaxResult.failed(500, "网络繁忙，请稍后重试！");
    }

}
